﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace MazeWars
{
    public class TitleWindow : Window
    {
        int curPos;
        int col;
        string title, story, options, createTree;
        SpriteFont titleFont;
        Vector2 titleFontPos, titleOrigin, storyFontPos, storyOrigin, optionsFontPos, optionsOrigin, createFontPos, createOrigin;
        Vector2[] cursorPos;
        Texture2D cursor;
        Viewport titleVP;
        Rectangle cursorSrcRect;

        const int TILE_WIDTH = 64;
        const int TILE_HEIGHT = 64;

        public TitleWindow()
        {
            col = 0;
            title = "Maze Wars Yo!!!";
            story = "Story";
            createTree = "Create Tree";
            options = "Tutorial";
            active = true;
            curPos = 0;
            cursorPos = new Vector2[3];
            cursorSrcRect = new Rectangle(col * TILE_WIDTH, 0, TILE_WIDTH, TILE_HEIGHT);
        }

        public override void setupWindow(ContentManager cm, Viewport vp)
        {
            titleFont = cm.Load<SpriteFont>("Times New Roman");
            cursor = cm.Load<Texture2D>("gamecursor");

            titleVP = vp;
            titleFontPos = new Vector2(vp.Width / 2, vp.Height / 4);
            titleOrigin = titleFont.MeasureString(title) / 2;
            storyFontPos = new Vector2(vp.Width / 2, vp.Height * 3 / 5);
            storyOrigin = titleFont.MeasureString(story) / 2;
            createFontPos = new Vector2(vp.Width / 2, vp.Height * 2 / 3);
            createOrigin = titleFont.MeasureString(createTree) / 2;
            optionsFontPos = new Vector2(vp.Width / 2, vp.Height * 11 / 15);
            optionsOrigin = titleFont.MeasureString(options) / 2;

            cursorPos[0] = new Vector2((vp.Width / 2) - createOrigin.Length() - 80,
                vp.Height * 3 / 5 - cursor.Height / 2);
            cursorPos[1] = new Vector2((vp.Width / 2) - createOrigin.Length() - 80,
                vp.Height * 2 / 3 - cursor.Height / 2);
            cursorPos[2] = new Vector2((vp.Width / 2) - createOrigin.Length() - 80,
                vp.Height * 11 / 15 - cursor.Height / 2);
        }

        public override void draw(SpriteBatch sb, GraphicsDevice gd)
        {
            gd.Viewport = titleVP;
            gd.Clear(Color.TransparentWhite);
            sb.Begin(SpriteBlendMode.AlphaBlend);

            sb.DrawString(titleFont, title, titleFontPos, Color.Black, 0, titleOrigin, 1.0f, SpriteEffects.None, 0);
            sb.DrawString(titleFont, story, storyFontPos, Color.Black, 0, storyOrigin, 1.0f, SpriteEffects.None, 0);
            sb.DrawString(titleFont, createTree, createFontPos, Color.Black, 0, createOrigin, 1.0f, SpriteEffects.None, 0);
            sb.DrawString(titleFont, options, optionsFontPos, Color.Black, 0, optionsOrigin, 1.0f, SpriteEffects.None, 0);
            sb.Draw(cursor, cursorPos[curPos], cursorSrcRect, Color.White);
            
            sb.End();
        }

        public override void update(KeyboardState ks, GameTime gameTime)
        {
            col++;
            col %= 59;
            cursorSrcRect.X = col * TILE_WIDTH;
            if(ks.IsKeyDown(Keys.Down) && prevState.IsKeyUp(Keys.Down))
            {
                curPos++;
                if(curPos > 2)
                    curPos = 0;
            }

            if(ks.IsKeyDown(Keys.Up) && prevState.IsKeyUp(Keys.Up))
            {
                curPos--;
                if(curPos < 0)
                    curPos = 2;
            }

            if (ks.IsKeyDown(Keys.Enter) && prevState.IsKeyUp(Keys.Enter))
            {
                switch (curPos)
                {
                    case 0: WindowManager.instance().changeWindow(Window.BATTLE_WINDOW); break;
                    case 1: WindowManager.instance().changeWindow(Window.CREATION_WINDOW); break;
                    case 2: WindowManager.instance().changeWindow(Window.TUTORIAL_WINDOW); break;
                    default: break;
                }
            }
            
            prevState = ks;
        }

        public override void loadWindow()
        {
            col = 0;
            curPos = 0;
        }
    }//End of class TitleWindow
}
